package com.trkj.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IHWCTbdepartmentDAO;

import com.trkj.pojos.Tbdepartment;



@Service
@Transactional
public class HwcDepartmentService {
	
	@Autowired
	private IHWCTbdepartmentDAO deptDAO;
	
	/**
	 * 分页显示科室信息
	 */
	public Map<String,Object> queryDepartmentPage(int page,int rows){
		PageRequest pr = new PageRequest(page -1,rows);
		//获取当前页记录
		List<Tbdepartment> list = deptDAO.findTbdepartmentPage(pr);
		//获取总的记录条数
		long total = deptDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	/**
	 * 分页显示科室名称模糊查询
	 */
	public Map<String,Object>queryDepartmentPageDname(int page,int rows,String departmentName){
		
		PageRequest pageble = new PageRequest(page -1,rows);
		//获取当前页记录
		List<Tbdepartment> list = deptDAO.findByTbdepartmentPageDepartmentName(pageble, departmentName);
		//获取总的记录数
		long total = deptDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	/**
	 * 查询所有科室
	 */
	public List<Tbdepartment> queryAllDepartment(){
		 List<Tbdepartment> list = deptDAO.queryAllTbdepartment();
		for(Tbdepartment t  : list){
			System.out.println(t);
		}
		
		return deptDAO.queryAllTbdepartment();
	}
	
	/**
	 * 添加科室
	 */
	public void addNewDept(Tbdepartment tbdepartment){
		deptDAO.save(tbdepartment);
		
	}
	/**
	 * 修改班级
	 */
	public void editTbDepartment(Tbdepartment tbdepartment){
		//System.out.print("Service 的ID"+tbdepartment.getDepartmentNo()+"名字"+tbdepartment.getDepartmentName()+tbdepartment.getAddress());
		deptDAO.save(tbdepartment);
	}
	/**
	 * 删除科室
	 */
	public void deleteTbDepartment(Long[] departmentnos){
		//将数组转换成集合
		DefaultConversionService service = 
				new DefaultConversionService();
		
		List<Long> list = service.convert(departmentnos, List.class);
		for(Long l : list){
			deptDAO.delete(l);
		}
	}
	/**
	 * 根据科室ID找到科室信息
	 */
	public Tbdepartment findTbdepartmentById(long departmentno){
		Tbdepartment department = deptDAO.findOne(departmentno);
		return department;
	}
	/**
	 * 属性复制
	 */
	private void copyTbDepartment(Tbdepartment src,Tbdepartment desc)
	{
		desc.setDepartmentNo(src.getDepartmentNo());
		desc.setDepartmentName(src.getDepartmentName());
		desc.setAddress(src.getAddress());
	}
	/**
	 * 根据部门名称查询
	 */
	public Map<String,Object>queryTbdepartmentByDName(String departmentName){
		List<Tbdepartment> list = deptDAO.findBydepartmentName(departmentName);
		Map<String,Object> map = new HashMap<>();
		map.put("total;", list.size());
		map.put("rows", list);
		
		return map;
		
	}
	/**
	 * 根据部门地址查询
	 */
	public Map<String,Object>queryTbdepartmentByAddress(String address){
		List<Tbdepartment> list = deptDAO.findByAddress(address);
		Map<String,Object>map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", list);
		
		return map;
	}
	/**
	 * 根据部门地址分页模糊查询
	 */
	public Map<String,Object> queryDAddressPage(int page,int rows,String DAddress){
		PageRequest pageble = new PageRequest(page -1,rows);
		//获取当前页信息
		
		List<Tbdepartment> list = deptDAO.findByAddressPage(pageble, DAddress);
		/*//获取总的记录数
		for(Tbdepartment tm :list){
			System.err.println("Service部门名称查询"+tm.getDepartmentName());
		}*/
		long total = deptDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total",total);
		map.put("rows", list);
		return map;
	}
}
